package com.example.listview;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDelListener {
    private ListView StuListView;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResultArrayList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btn_addstu;
    private EditText edtKeyWord;
    private Button btnSearch;

    private boolean isResultList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initStuArrayList();

        initAdapter();

        initListView();

        initBtnAddStu();

        initSearchView();
    }



    /**
     * 初始化适配器
     */
    private void initAdapter() {
        StuListView = findViewById(R.id.Stu_ListView);
        adapter = new StuAdapter(MainActivity.this, studentArrayList, MainActivity.this);
        StuListView.setAdapter(adapter);
    }

    /**
     * 初始化ListView
     */
    private void initListView() {

        StuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
                Student s = studentArrayList.get(i);

                if(isResultList){
                    s=searchResultArrayList.get(i);
                }
                Toast.makeText(MainActivity.this,
                        "姓名" + s.getName() +
                                "年龄" + s.getAge(), Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(MainActivity.this, EditActivity.class);

                intent.putExtra("Name", s.getName());
                intent.putExtra("Age", s.getAge());
                intent.putExtra("ImgId", s.getImgId());
                intent.putExtra("NO",s.getNo());

                startActivityForResult(intent, 1002);

            }

        });

    }

    /**
     * 初始化添加学生按钮BtnAddStu
     */
    private void initBtnAddStu() {
        btn_addstu = findViewById(R.id.Btn_AddStu);
        btn_addstu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 2001);
            }
        });
    }

    /**
     * 初始化搜索模块
     */
    private void initSearchView() {
        edtKeyWord=findViewById(R.id.edt_keyword);
        btnSearch=findViewById(R.id.btn_search);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword=edtKeyWord.getText().toString().trim();
                //使用学生列表和关键字 得到搜索结果列表
                search(studentArrayList,keyword);
                adapter.ChangeData(searchResultArrayList);
                isResultList=true;
            }
        });
        edtKeyWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword=edtKeyWord.getText().toString().trim();
                //使用学生列表和关键字 得到搜索结果列表
                search(studentArrayList,keyword);
                adapter.ChangeData(searchResultArrayList);
                isResultList=true;
            }
        });
    }

    /**
     * 使用学生列表和关键字 得到搜索结果列表
     * @param studentArrayList
     * @param keyword
     */
    private void search(ArrayList<Student> studentArrayList, String keyword) {
        searchResultArrayList.clear();
        for(int i=0;i<studentArrayList.size();i++){
            Student s=studentArrayList.get(i);
            if(s.getName().contains(keyword)){
                searchResultArrayList.add(s);
            }
            else if(String.valueOf(s.getAge()).equals(keyword)){
                searchResultArrayList.add(s);
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.e("MainActivity", "requestCode=" + requestCode + "resultCode=" + requestCode);

        if (data == null) {
            return;
        }

        if (resultCode == 2001) {

            String name = data.getStringExtra("Name");
            int age = data.getIntExtra("Age", 0);
            int imgId = data.getIntExtra("Img", R.drawable.dog1);


            Student student = new Student("10011",name, age, imgId);
            studentArrayList.add(student);
            adapter.notifyDataSetChanged();
        }

        else if(resultCode == 3001){
            String name = data.getStringExtra("Name");
            int age = data.getIntExtra("Age", 0);
            int imgId = data.getIntExtra("Img", R.drawable.dog1);
            String no=data.getStringExtra("NO");

            for(int i=0;i<studentArrayList.size();i++){
                Student s=studentArrayList.get(i);

                if(s.getNo().equals(no)){
                    s.setName(name);
                    s.setAge(age);
                    s.setImgId(imgId);
                }
            }
            String keyword=edtKeyWord.getText().toString().trim();
            //使用学生列表和关键字 得到搜索结果列表
            search(studentArrayList,keyword);
            adapter.ChangeData(searchResultArrayList);
            isResultList=true;

        }

    }

    private void initStuArrayList() {
        Student s1 = new Student("10086","甲", 1, R.drawable.dog1);
        Student s2 = new Student("10087","乙", 2, R.drawable.dog2);
        Student s3 = new Student("10088","丙", 3, R.drawable.dog3);
        Student s4 = new Student("10089","丁", 4, R.drawable.dog4);
        Student s5 = new Student("10010","它", 5, R.drawable.dog5);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    public void del(int i) {
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
